using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace Acms.AcmsControls
{
    [ToolboxData("<{0}:HtmlContentControl runat=server></{0}:HtmlContentControl>")]
    public class HtmlContentControlDisplay : BaseContentControlDisplay
    {
        private HtmlContentControlProperties properties;
        protected HtmlContentControlProperties Properties
        {
            get 
            {
                if (properties == null)
                {
                    properties = new HtmlContentControlProperties();
                }
                return properties;
            }
        }

        private string text;

        protected override BaseContentControlProperties GetProperties()
        {
            return Properties;
        }

        protected override void SetProperties(BaseContentControlProperties properties)
        {
            this.properties = (HtmlContentControlProperties)properties;
            if (this.properties != null)
            {
                text = this.properties.GetHtml();
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (text != null)
            {
                writer.Write(Highlight(text));
            }
        }

        protected override object SaveViewState()
        {
            ViewState["Text"] = text;
            ViewState["HighlightText"] = highlightText;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            text = ViewState["Text"] as string;
            highlightText = ViewState["HighlightText"] as string;
        }

        private string highlightText;
        public string HighlightText
        {
            get { return highlightText; }
            set { highlightText = value; }
        }

        private string Highlight(string text)
        {
            if (text != null)
            {
                Control current = this;
                BaseTemplateControl container = null;
                while (container == null && current != null && current.NamingContainer != null)
                {
                    current = current.NamingContainer;
                    container = current as BaseTemplateControl;
                }
                if (container != null && !string.IsNullOrEmpty(container.HighlightText))
                {
                    // Setup the regular expression and add the Or operator.
                    Regex regExp = new Regex(string.Concat("\\b", container.HighlightText.Replace(" ", "|").Trim()), RegexOptions.IgnoreCase);
                    // Highlight keywords by calling the delegate each time a keyword is found.
                    return regExp.Replace(text, new MatchEvaluator(ReplaceKeyWords));
                }
                else
                {
                    return text;
                }
            }
            else
            {
                return text;
            }
        }

        private string ReplaceKeyWords(Match m)
        {

            return "<span class=\"searchHighlight\">" + m.Value + "</span>";

        }

    }
}
